package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func PathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	} else if os.IsNotExist(err) {
		return false, nil
	} else {
		return false, err
	}
}
func CopyFile(dstFile string, srcFile string) (written int64, err error) {
	src, err := os.Open(srcFile)
	if err != nil {
		fmt.Println("open srcfile error", err)
		return
	}
	reader := bufio.NewReader(src)
	defer src.Close()
	dst, err := os.OpenFile(dstFile, os.O_WRONLY|os.O_CREATE, 0666)
	if err != nil {
		fmt.Println("open dst file error", err)
		return
	}
	writer := bufio.NewWriter(dst)
	defer dst.Close()

	return io.Copy(writer, reader)

}

func main() {

	s := "./hexing.com/project1/test5/filemain.go"
	d := "./hexing.com/project1/test5/newmain.go"
	_, err := CopyFile(d, s)
	if err != nil {
		fmt.Println("copy file error", err)
	}
	// stats, err := PathExists(s)
	// fmt.Printf("status:%v,err:%v\n", stats, err)
}
